package com.rs.controller;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.border.EmptyBorder;

import com.rs.model.CommonResult;
import com.rs.model.ZhClasses;
import com.rs.model.ZhLeanprogress;
import com.rs.model.ZhUser;
import com.rs.service.ZhClassesService;
import com.rs.service.ZhLeanprogressService;
import com.rs.service.ZhUserService;
import com.rs.service.ZhUserStudentService;
import com.rs.util.DateChooserJButton;
import com.rs.util.SpringContextUtils;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ClassInfo extends JFrame {
	
	 protected static final JDesktopPane JDesktopPane = null;

	JDesktopPane desktopPane_1;
	
	private int state=1;  //1为 添加  2 为修改
	private int id; //班级id

	private JPanel contentPane;
	private JTextField text_name;
	private JTextField text_deginTime;
	private JTextField text_endTime;
	private JComboBox combo_teacher;
	private JComboBox combo_jd;
	
	JPanel panel_deginTime;
	JPanel panel_endTime;

	SpinnerDateModel model = new SpinnerDateModel();
    JSpinner year = new JSpinner(model);
	
	private ZhClassesService classesService = SpringContextUtils.getBean(ZhClassesService.class);
	private ZhUserStudentService teacherService = SpringContextUtils.getBean(ZhUserStudentService.class);
	private ZhLeanprogressService lea = SpringContextUtils.getBean(ZhLeanprogressService.class);
	 DateChooserJButton dc= new DateChooserJButton();
	 DateChooserJButton dc2= new DateChooserJButton();
	 
	 Map<String,Integer> mapJd = new HashMap<>();
	 Map<String,Integer> mapTea = new HashMap<>();
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClassInfo frame = new ClassInfo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClassInfo() {	
		aa();
	}
	public ClassInfo( final JDesktopPane desktopPane_1) {
	
		aa();
	}
	
	public ClassInfo( final JDesktopPane desktopPane_1,Integer id,Integer state) {
	
		this.desktopPane_1 = desktopPane_1;
		this.id=id;
		this.state=state;
		aa();
	}

	void aa(){
//		setClosable(true);
//		setMaximizable(true);
//		setIconifiable(true);
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 511, 382);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label_name = new JLabel("班级名称：");
		label_name.setBounds(38, 74, 73, 15);
		contentPane.add(label_name);
		
		JLabel label_userName = new JLabel("教师：");
		label_userName.setBounds(38, 128, 73, 15);
		contentPane.add(label_userName);
		
		JLabel label_jd = new JLabel("学习进度：");
		label_jd.setBounds(38, 181, 73, 15);
		contentPane.add(label_jd);
		
		JLabel label_deginTime = new JLabel("开班时间：");
		label_deginTime.setBounds(240, 74, 73, 15);
		contentPane.add(label_deginTime);
		
		
		
		text_name = new JTextField();
		text_name.setBounds(103, 71, 104, 21);
		contentPane.add(text_name);
		text_name.setColumns(10);
		
		
		
		
		/*text_deginTime = new JTextField();
		text_deginTime.setColumns(10);
		text_deginTime.setBounds(298, 71, 104, 21);
		contentPane.add(text_deginTime);*/
		
		 panel_deginTime = new JPanel();
		 panel_deginTime.setBounds(298, 71, 104, 21);
		 contentPane.add(panel_deginTime);
		 panel_deginTime.add(dc);
		   
		 /*year.setValue(new Date());	    
	     JSpinner.DateEditor editor = new JSpinner.DateEditor(year,"yyyy-MM-dd");
	     year.setEditor(editor);
	     year.setBounds(298, 71, 104, 21);*/
		
		/*text_endTime = new JTextField();
		text_endTime.setColumns(10);
		text_endTime.setBounds(298, 125, 104, 21);
		contentPane.add(text_endTime);*/
		 
		 
		
		 
		JLabel label = new JLabel("班级信息");
		label.setFont(new Font("宋体", Font.PLAIN, 16));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(210, 25, 73, 21);
		contentPane.add(label);
		
		combo_teacher = new JComboBox();
		combo_teacher.setBounds(103, 125, 104, 21);
		contentPane.add(combo_teacher);
		setTeacherComboBox();
		
		combo_jd = new JComboBox();
		combo_jd.setBounds(103, 178, 104, 21);
		contentPane.add(combo_jd);
		setJdComboBox();
		
		
		if(state ==1){
			JButton btn_submit = new JButton("提交");
			btn_submit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ZhClasses cl = new ZhClasses();
					cl.setClName(text_name.getText());
					cl.setClUid(mapTea.get(combo_teacher.getSelectedItem()));
					cl.setClLpgid(mapJd.get(combo_jd.getSelectedItem()));
					cl.setClTime(dc.getDate());
					//cl.setClEndtime(dc2.getDate());
					classAdd(cl);
				}
				
			});
			btn_submit.setFont(new Font("宋体", Font.PLAIN, 14));
			btn_submit.setBounds(312, 243, 93, 34);
			contentPane.add(btn_submit);
		}else if(state ==2){
			
			JLabel label_endTime = new JLabel("结业时间：");
			label_endTime.setBounds(240, 128, 73, 15);
			contentPane.add(label_endTime);
			
			panel_endTime = new JPanel();
			 panel_endTime.setBounds(298, 125, 104, 21);
			 contentPane.add(panel_endTime);
			 panel_endTime.add(dc2);
			
			 setTheValue();
			JButton btn_update = new JButton("提交修改");
			btn_update.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ZhClasses cl = new ZhClasses();
					cl.setClName(text_name.getText());
					cl.setClUid(mapTea.get(combo_teacher.getSelectedItem()));
					cl.setClLpgid(mapJd.get(combo_jd.getSelectedItem()));
					cl.setClTime(dc.getDate());
					cl.setClEndtime(dc2.getDate());
					cl.setClId(id);
					classUpdate(cl);
					
					SelectClass cll= new SelectClass(desktopPane_1);
					cll.setVisible(true);
					desktopPane_1.removeAll();
					desktopPane_1.add(cll);
				}
			});
			btn_update.setBounds(309, 227, 93, 34);
			contentPane.add(btn_update);
		}
		
		
		
	}
	
	private ZhClasses getClassInfo(Integer clId){
		return classesService.selectByPrimaryKey(clId);
	}
	
	//修改时  付默认值
	private void setTheValue(){
		ZhClasses cl = classesService.selectByPrimaryKey(id);
		text_name.setText(cl.getClName());
		System.out.println("hhhhhhhhhhhhhhhh"+getTeacherName(cl.getClUid())+"    "+getLeaName(cl.getClLpgid()));
		combo_teacher.setSelectedItem(getTeacherName(cl.getClUid()));
		combo_jd.setSelectedItem(getLeaName(cl.getClLpgid()));
		dc.setDate(cl.getClTime());
		dc2.setDate(cl.getClEndtime());
	}
	
	
	//添加信息提交
	private void classAdd(ZhClasses cla){
		CommonResult<Integer> com = classesService.insertSelective(cla);
		JOptionPane.showMessageDialog(this, com.getMessage());
		if(com.getData()==1){
			this.dispose();
		}
	}
	//修改信息
	private void classUpdate(ZhClasses cla){
		CommonResult<Integer> com = classesService.updateByPrimaryKeySelective(cla);
		JOptionPane.showMessageDialog(this, com.getMessage());
		if(com.getData()==1){
			this.dispose();
		}
	}
	
	//得到所有的教师并放入下拉框
	private void setTeacherComboBox(){
		List<ZhUser> lsit = teacherService.listTeachers("教师");
		for(ZhUser uu:lsit){
			combo_teacher.addItem(uu.getuName());
			mapTea.put(uu.getuName(), uu.getuId());
		}
	}
	
	//得到所有学习进度并放入下拉框
	private void setJdComboBox(){
		List<ZhLeanprogress> ll = lea.selectZhLeanprogress(null);
		for(ZhLeanprogress le : ll){
			combo_jd.addItem(le.getLpgTitle());
			mapJd.put(le.getLpgTitle(),le.getLpgId());
		}
	}
	
	//教师根据id得到名字
	private String getTeacherName(Integer uid){
		ZhUser user = new ZhUser();
		user.setuId(uid);
		CommonResult<ZhUser> com = teacherService.selectStudent(user);
		ZhUser uu = com.getData();
		return uu.getuName();
	}
	
	//根据教学进度id得到标题
	private String getLeaName(Integer leId){
		ZhLeanprogress ll = lea.selectZhLean(leId);
		return ll.getLpgTitle();
		
	}
	
}
